<?php
class SouhuController extends Controller {

	private function souhuini(){
		//获取token
		session_start();
		$souhu_token_model = $this->model('souhu_token');
		$tokendata = $souhu_token_model->from('souhu_token')->where('userid=?', $_SESSION['userid'])->select();
		
		if(empty($tokendata)){
			$url = $this->create_url('souhu/authorize');
			$this->show_message('用户还没对搜狐微博授权，即将转入搜狐微博授权页面',$url,1 );
		}

		set_include_path(dirname(dirname(__FILE__)) . '/extensions/souhu/');
		require_once('oauth/SohuOAuth.php');
		require_once('config.php');
		/* 使用token创建SohuOauth对象*/
		$oauth = new SohuOAuth(CONSUMER_KEY, CONSUMER_SECRET, $tokendata[0]['access_token'], $tokendata[0]['oauth_token_secret']);

		return $oauth;
	}

	public function indexAction() {
		$c = $this->souhuini();
		$url = 'http://api.t.sohu.com/statuses/public_timeline.json';
		$content = $c->get($url);
		$this->assign('homemessage' , $content);
		$this->display();

	}

	public function authorizeAction(){
		/* Load and clear sessions */
		session_start();
		set_include_path(dirname(dirname(__FILE__)) . '/extensions/souhu/');
		require_once('config.php');
		if (CONSUMER_KEY === '' || CONSUMER_SECRET === '') {
		  echo '请先设置Consumer Key以及对于的Consumer Key Secret。如果还没有的话，可以到<a href="http://open.t.sohu.com/index.jsp">http://open.t.sohu.com/index.jsp</a>申请。';
		  exit;
		}
		/* 开始申请access token */
		$content = '<a href="./redirect.php">申请Access Token</a>';
		 
		/* Include HTML to display on the page. */
		include('html.inc');
		require_once('oauth/SohuOAuth.php');

		/* 创建SohuOAuth对象 */
		$oauth = new SohuOAuth(CONSUMER_KEY, CONSUMER_SECRET);
		 
		/* 获取request token */
		$request_token = $oauth->getRequestToken(OAUTH_CALLBACK);

		/* 保存request token，成功获取access token之后用access token代替 */
		$_SESSION['oauth_token'] = $token = $request_token['oauth_token'];
		$_SESSION['oauth_token_secret'] = $request_token['oauth_token_secret'];
		 
		switch ($oauth->http_code) {
		  case 200:
		    /* 获取用户认证地址，并且重定向到SOHU */
		    $url = $oauth->getAuthorizeUrl1($token, OAUTH_CALLBACK);
		    // header('Location: ' . $url); 
		    echo '<a href = "'.$url.'" >去授权吧</a>';
		    break;
		  default:
		    /* Show notification if something went wrong. */
		    echo '出错了。。。请重试。';
		}
	}

	//搜狐callbak
	public function callbackAction(){
		/* Start session and load lib */
		session_start();
		set_include_path(dirname(dirname(__FILE__)) . '/extensions/souhu/');
		require_once('oauth/SohuOAuth.php');
		require_once('config.php');

		/* Create TwitteroAuth object with app key/secret and token key/secret from default phase */
		$connection = new SohuOAuth(CONSUMER_KEY, CONSUMER_SECRET, $_SESSION['oauth_token'], $_SESSION['oauth_token_secret']);

		/* Request access tokens from twitter */
		$access_token = $connection->getAccessToken($_REQUEST['oauth_verifier']);

		/* Save the access tokens. Normally these would be saved in a database for future use. */
		$_SESSION['access_token'] = $access_token;

		//token 入库
		$souhu_token_model = $this->model('souhu_token');
		$souhu_token_data = array();
		$souhu_token_data['userid'] = $_SESSION['userid'] ;
		$souhu_token_data['access_token'] = $access_token['oauth_token'];
		$souhu_token_data['oauth_token_secret'] = $access_token['oauth_token_secret'];

		if($souhu_token_model->insert($souhu_token_data)){
			/* Remove no longer needed request tokens */
			unset($_SESSION['oauth_token']);
			unset($_SESSION['oauth_token_secret']);
			
			$url = $this->create_url('souhu/index');
			$this->show_message('授权成功，即将转入搜狐微博',$url,1 );
		}else{
			$url = $this->create_url('index/index');
			$this->show_message('授权失败，回到首页',$url,1 );
		}

	}
}